package com.sphd.emanu.activity.addgoodfrom;

import android.content.Context;

import com.google.gson.JsonObject;
import com.sphd.emanu.mvp.BasePresenterImpl;
import com.sphd.emanu.pojo.CategoryAndStorageEntity;
import com.sphd.emanu.pojo.goods.Category;
import com.sphd.emanu.utils.GsonUtil;
import com.wzutils.L;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * MVPPlugin
 *  邮箱 784787081@qq.com
 */

public class AddGoodFromPresenter extends BasePresenterImpl<AddGoodFromContract.View> implements AddGoodFromContract.Presenter{

    @Override
    public void selectNextCategory(Integer id) {
        emanuService.selectNextCategory(id).enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                L.e(response.toString());
                Category category = GsonUtil.GsonToBean(response.body().toString(),Category.class);
                mView.setCate(category);
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Error(t);
            }
        });
    }

    @Override
    public void categorAndStorage(int org, Integer id) {
        Call<String> entityCall=emanuService.categorAndStorage(org,id);
        entityCall.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                L.e(response.toString());
                CategoryAndStorageEntity categoryAndStorageEntity= GsonUtil.GsonToBean(response.body(),CategoryAndStorageEntity.class);
                mView.setEntity(categoryAndStorageEntity);
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Error(t);
            }
        });
    }

    @Override
    public void addComposition(int pid,String name, String specifications, String categoryName, Integer amount,int mId) {
        Call<JsonObject> call =emanuService.addComposition(pid,name,specifications,categoryName,amount,mId);
        call.enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                L.e(response.toString());
                mView.setRes(res(response));
            }

            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {
            Error(t);
            }
        });
    }

    @Override
    public void updateComposition(int id, String name, String specifications, String model, Integer amount) {
        Call<JsonObject> call =emanuService.updateComposition(id,name,specifications,model,amount);
        call.enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                mView.setRes(res(response));
            }

            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {
                Error(t);
            }
        });
    }
}
